/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SLE_HOST_HDI_H
#define SLE_HOST_HDI_H

#include "sle_hal_errcode.h"
#include "sle_device_config.h"

/**
 * @brief Callback invoked when SLE stack enable.
 * 
 * @attention 1.This function is called in SLE service context,should not be blocked or do long time waiting.
 * @attention 2.The memories of pointer are requested and freed by the SLE service automatically.
 * @param [in] status error code.
 * @return None
 */
typedef void (*SleHdiEnableSleCallback)(SleHdiErrorCode errCode);

/**
 * @brief Callback invoked when SLE stack disable.
 * 
 * @param [in] status error code.
 * @return None
 */
typedef void (*SleHdiDisableSleCallback)(SleHdiErrorCode errCode);

/**
 * @brief Struct of SLE device host callback function.
 */
typedef struct {
    SleHdiEnableSleCallback OnSleEnabledCb;                      /*!< @if Eng SLE stack enable callback.
                                                                 @else   SLE协议栈使能回调函数。 @endif */
    SleHdiDisableSleCallback OnSleDisabledCb;                    /*!< @if Eng SLE stack disable callback.
                                                                 @else   SLE协议栈去使能回调函数。 @endif */
} SleHostHdiCallbacks;

/**
 * @brief  Enable SLE stack.
 * 
 * @param NULL
 * @return error code.
 */
typedef SleHdiErrorCode (*SleHdiEnableSle)(void);

/**
 * @brief  Disable SLE stack.
 * 
 * @param NULL
 * @return error code.
 */
typedef SleHdiErrorCode (*SleHdiDisableSle)(void);

/**
 * @brief  Set host device address.
 * 
 * @param [in] addr address.
 * @return error code.
 */
typedef SleHdiErrorCode (*SleHdiSetHostAddress)(const SleDeviceAddress *addr);

/**
 * @brief  Get host device address.
 * 
 * @param [out] addr address.
 * @return error code.
 */
typedef SleHdiErrorCode (*SleHdiGetHostAddress)(SleDeviceAddress *addr);

/**
 * @brief Use this funtion to set host device name.
 * 
 * @param  [in] name host device name.
 * @param  [in] len  host device name length.
 * @return error code.
 */
typedef SleHdiErrorCode (*SleHdiSetHostName)(const uint8_t *name, uint8_t len);

/**
 * @brief Use this funtion to get host device name.
 * 
 * @param   [out]   name host device name.
 * @param   [inout] len  as input parameter, the buffer size of user,
                         as output parameter, the length of host device name.
 * @return error code
 */
typedef SleHdiErrorCode (*SleHdiGetHostName)(uint8_t *name, uint8_t *len);

/**
 * @brief Use this funtion to register host device callback.
 * 
 * @param  [in] hostCallbacks host device callback function.
 * @return error code.
 */
typedef SleHdiErrorCode (*SleHdiRegisterHostCallback)(SleHostHdiCallbacks *hostCallbacks);

/**
 *  @brief  read data in the na partition
 * 
 *  @param  [in] key -  key id of the nv item to be read
 *  @param  [in] kvalue_max_length -  maximum length allowed for storing data
 *  @param  [out] kvalue_length -  actual length of data read
 *  @param  [out] kvalue -  pointer to the buffer that stores and reads data
 *  @return SleErrorCode - operation result
 */
typedef SleHdiErrorCode (*SleHdiUapiNvRead)(uint16_t key, uint16_t kvalue_max_length, uint16_t *kvalue_length, uint8_t *kvalue);

/**
 *  @brief  write nv data item
 * 
 *  @param  [in] key -  key id of the nv item to be write
 *  @param  [in] kvalue -  pointer to the value of the NV item to be written
 *  @param  [in] kvalue_length -  The length of data written (in bytes)
 *  @return SleErrorCode - operation result
 */
typedef SleHdiErrorCode (*SleHdiUapiNvWrite)(uint16_t key, uint8_t *kvalue, uint16_t kvalue_length);

/**
 * @brief Struct of host module function.
 */
typedef struct {
    SleHdiEnableSle enableSle;                                              /*!< @if Enable SLE stack.
                                                                                  @else   使能SLE协议栈。 @endif */
    SleHdiDisableSle disableSle;                                            /*!< @if Disable SLE stack.
                                                                                  @else   关闭SLE协议栈。 @endif */
    SleHdiSetHostAddress setHostAddress;                                    /*!< @if Set host device address.
                                                                                  @else   设置本地设备地址。 @endif */
    SleHdiGetHostAddress getHostAddress;                                    /*!< @if Get host device address.
                                                                                  @else   获取本地设备地址。 @endif */
    SleHdiSetHostName setHostName;                                          /*!< @if Set host device name.
                                                                                  @else   设置本地设备名称。 @endif */
    SleHdiGetHostName getHostName;                                          /*!< @if Get host device name.
                                                                                  @else   获取本地设备名称。 @endif */
    SleHdiRegisterHostCallback registerHostCbks;                            /*!< @if Register SLE device host callbacks.
                                                                                  @else   注册SLE设备发现回调函数。 @endif */
    SleHdiUapiNvRead uapiNvRead;                                            /*!< @if nv read.
                                                                                  @else   读配置项参数。 @endif */    
    SleHdiUapiNvWrite uapiNvWrite;                                          /*!< @if nv write.
                                                                                  @else   写配置项参数。 @endif */  
                                                                      
} SleHostHdiOpts;

/** 
 * @brief   Get SLE host hdi opts.
 * 
 * @param   [out] hostOpts SLE host hdi opts.
 * @return  SleHdiErrorCode
 * 
 * @note    hostOpts should not be NULL and should be deleated by user 
 */
SleHdiErrorCode GetSleHostHdiOpts(SleHostHdiOpts *hostOpts);

#endif // SLE_HOST_HDI_H